<?php

/***
 * 描述：订单统计模型
 * @category		Lib
 * @package			Model			
 * @subpackage	
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-7-30
 * @version			$Id$
 */
class OrderStatModel {
	public static $ORDER_STATUS_WC = 0;
	public static $ORDER_STATUS_XJ = 1;
	public static $ORDER_STATUS_PH = 2;
	public static $ORDER_STATUS_PS = 3;
	public static $ORDER_STATUS_TH = 4;

	public static $CUST_ORDER_STATUS_ZC = 0;
	public static $CUST_ORDER_STATUS_THPART = 1;
	public static $CUST_ORDER_STATUS_THALL = 2;
	public static $CUST_ORDER_STATUS_CANCEL = 3;
	/**
	 * 描述：查询指定商铺，指定状态的订单
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string 	$shop_id		商铺ID
	 * @param 		int		$order_status	订单状态
	 * @param 		array	$search			其他搜素条件
	 * @param 		string 	$ordlist		排序
	 * @since		2013-4-21
	 */
	public static function statOrder($shop_id=0,$search=array()){
		$mod = new CommonModel('OrderLib');
		$modWhere = array(
			'shop_id' => $shop_id,
		);
		
		if($search['order_type']!= null){
			$modWhere['order_type'] = $search['order_type'];
		}
		if($search['order_status']!= null){
			$modWhere['order_status'] = $search['order_status'];
		}
		if($search['cust_order_status']!= null){
			$modWhere['cust_order_status'] = array('in',$search['cust_order_status']) ;
		}
		
		if($search['cust_order_time_start'] && $search['cust_order_time_end']){
			$modWhere['cust_order_time'] = array('between',array(strtotime($search['cust_order_time_start']),strtotime($search['cust_order_time_end'])+86400));
		}
		$fields = 'count(*) as order_cnt,sum(order_amt) as order_amt_sum,sum(order_send_fee) as order_send_fee_sum,sum(cust_pay_amt) as cust_pay_amt_sum,order_status,cust_order_status,order_type';
		$group = 'order_type,order_status,cust_order_status';
		$list = $mod->field($fields)->where($modWhere)->group($group)->select();
		$custPayAmt = 0;
		$custPayAmt = 0;
		$orderSendFeeAmt = 0;
		$orderCnt = 0;
		foreach ($list as $key=>$value){
			//1.计算客户支付总金额
			$orderAmt += $value['order_amt_sum'];
			$orderSendFeeAmt += $value['order_send_fee_sum'];
			$custPayAmt += $value['cust_pay_amt_sum'];
			$orderCnt += $value['order_cnt'];
		}
		$rtnData['list']=$list;
		$rtnData['orderAmt'] = $orderAmt;
		$rtnData['orderSendFeeAmt'] = $orderSendFeeAmt;
		$rtnData['custPayAmt'] = $custPayAmt;
		$rtnData['orderCnt'] = $orderCnt;
		return $rtnData;	
	}
	/**
	 * 描述：查询指定商铺，统计商品销售
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string 	$shop_id		商铺ID
	 * @param 		int		$order_status	订单状态
	 * @param 		array	$search			其他搜素条件
	 * @param 		string 	$ordlist		排序
	 * @since		2013-4-21
	 */
	public static function statProduct($shop_id=0,$search=array()){
		$mod = new Model();
		$modWhere = array(
				'shop_id' => $shop_id,
		);
		$where = ' where shop_id = '.$shop_id;
		if($search['order_type']!= null){
			$where .= " and d.order_type = ".$search['order_type'];
		}
		if($search['order_status']!= null){
			$where .= " and l.order_status = ".$search['order_status'];
		}
		if($search['cust_order_status']!= null){
			$where .= " and l.cust_order_status = ".$search['cust_order_status'];
		}
	
		if($search['cust_order_time_start'] && $search['cust_order_time_end']){
			$where .= " and l.cust_order_time between ".strtotime($search['cust_order_time_start'])." and ".(strtotime($search['cust_order_time_end'])+86400);
		}
		
		$fields = ' select d.order_type,d.prod_id,count(d.prod_id) as prod_cnt,d.prod_name,d.prod_spec,d.prod_spec_qty,d.prod_spec_unit,sum(d.prod_num-d.prod_cancel_num) as sale_cnt,sum((d.prod_num-d.prod_cancel_num)*prod_price_real) as sale_amt
		';
		$table  = ' from order_lib l inner join order_detail d on l.order_code=d.order_code';
		$group  = ' group by d.prod_id,d.order_type';
		if($search['order_by']==2){
			$order  = ' order by sale_amt desc';
		}else{
			$order  = ' order by sale_cnt desc';
		}
		
		$list = $mod->query($fields.$table.$where.$group.$order);
		$saleCnt = 0;
		$saleAmt = 0;
		foreach ($list as $key=>$value){
			//1.计算客户支付总金额
			$saleCnt += $value['sale_cnt'];
			$saleAmt += $value['sale_amt'];
		}
		$rtnData['list']=$list;
		$rtnData['saleCnt'] = $saleCnt;
		$rtnData['saleAmt'] = $saleAmt;
		return $rtnData;
	}
	

}

?>